<?php
/**
 * Created by PhpStorm.
 * User: FDG367
 * Date: 2018/02/08
 * Time: 15:33
 * detail:商城导览楼层分类表 510
 */
namespace common\models;

use common\helps\StringHelper;
use Yii;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

class FloorCategory extends ActiveRecord{

	const USE_FLG_YES	= 1;
	const USE_FLG_NO	= 0;
    //楼层分类等级  分类等级 1：一级 2：二级
    const FLOOR_CATEGORY_ONE = 1;
    const FLOOR_CATEGORY_TWO = 2;

    public static function tableName(){
        return '{{%floor_category}}';
    }

	/**
	 * @param $floor 根据楼层查询楼层的基本信息
	 * @return array|\yii\db\ActiveRecord[]
	 */
	public static function getFloorCategoryInfo($floor=SHOPPING_MALL_FLOOR_0){
		$params	=array_filter([
			'floor'		=>$floor,
		]);

		return self::find()
			->where($params)
			->orderBy(['floor_num'=>SORT_DESC])
			->all();
	}

    //手机端获取全部楼层信息
    public static function getFloorListOnMobile(){

        $category_list = self::find()->where(['category_grade'=>self::FLOOR_CATEGORY_ONE])->orderBy('floor_num desc')->asArray()->all();
        $child_cate_list = array();

        $child_cate = self::find()->where(['category_grade'=>self::FLOOR_CATEGORY_TWO])->select('floor_num,content')->asArray()->all();
        foreach($child_cate as $value){
            $child_cate_list[$value['floor_num']] = $value;
        }

        //品牌列表
        $brand_plus_model = new BrandPlus();
        $brand_plus_model->ntoArray = 1;
        $brand = $brand_plus_model->getBrandPlusInfo();
        $brand_list = array();
        foreach($brand as $br){
            $brand_list[$br['floor_num']][] = $br;
        }

        foreach($category_list as &$cate){
            $cate['child_cate'] = empty($child_cate_list[$cate['floor_num']]['content']) ? '' : $child_cate_list[$cate['floor_num']]['content'];
            $cate['brand_list'] = empty($brand_list[$cate['floor_num']]) ? array() : $brand_list[$cate['floor_num']];
        }
        return $category_list;
    }

	/*
	 * @param 楼层分组
	 */
	public static function getFloorGroup(){
		$groupInfo	= [];
		$floorInfo	= self::getFloorCategoryInfo();

		if(0>=count($floorInfo)){
			return false;
		}

		foreach($floorInfo as $value){
			$groupInfo[$value->floor_num]['id']	= $value->id;
			$groupInfo[$value->floor_num]['info'][$value->category_grade]	= $value;
			$groupInfo[$value->floor_num]['floor']							= $value->floor;
			$groupInfo[$value->floor_num]['floor_num']						= $value->floor_num;
			$groupInfo[$value->floor_num]['floor_num']						= $value->floor_num;
			$groupInfo[$value->floor_num]['content'.$value->category_grade] = $value->content;
			//$groupInfo[$value->floor_num][''] = $value->content; 获取当前楼层的钱几个品牌
			$groupInfo[$value->floor_num]['brand_plus'] = (new BrandPlus())->getBrandPlusInfo($value->floor);
		}

		return $groupInfo;
	}
}